package com.onpositive.notes.impl;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.Locale;

import com.onpositive.notes.model.INote;
import com.onpositive.notes.model.INoteEntry;
import com.onpositive.notes.parser.Block;

public class BasicNote implements INote {

	public BasicNote() {
		super();
	}

	private static final long serialVersionUID = 4316937427732422022L;
	
	private ArrayList<INoteEntry> entries ;
	private Date date ;
	private int id = -1 ;
	private String initialText ;
	private ArrayList<Block<?>> blockList ;
	
	public BasicNote(Collection<INoteEntry> entries, Collection<Block<?>> blocks, Date date, String text) {
		super();
		if(entries != null)
			this.entries = new ArrayList<INoteEntry>(entries);
		else
			this.entries = new ArrayList<INoteEntry>() ;
		
		this.date = date ;
		this.initialText = text ;
		
		if( blocks != null )
			this.blockList = new ArrayList<Block<?>>( blocks ) ;
		else
			this.blockList = new ArrayList<Block<?>>() ;
	}

	@Override
	public String getText(Locale locale) {
		
		if( entries == null )
			return "NULL" ;
		
		StringBuffer buf = new StringBuffer() ;
		for( INoteEntry e : entries )
		{
			String text = e != null ? e.getText(locale) : "NULL" ;
			buf.append(text) ;
		}
		return buf.toString() ;
	}

	@Override
	public List<INoteEntry> getEntries() {
		return entries ;
	}

	@Override
	public void setEntries(List<INoteEntry> entries) {
		this.entries = new ArrayList<INoteEntry>(entries);
	}

	@Override
	public String getText() {
		return getText(Locale.getDefault());
	}
	
	public String toString(){
		return getText() ;
	}

	@Override
	public Date getDate() {
		return date;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
		for( INoteEntry entry : entries )
			entry.setNoteId(id) ;		
	}

	@Override
	public String getInitialText() {
		return initialText ;
	}

	@Override
	public List<Block<?>> getBlocks() {
		return blockList ;
	}
	
	public void copyContent(INote another){
		this.entries = new ArrayList<INoteEntry>(another.getEntries() ) ;
		this.blockList = new ArrayList<Block<?>>(another.getBlocks() ) ;
		this.initialText = another.getInitialText() ;
		this.date = another.getDate() ;
	}

}
